package Stream;


import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.stream.Stream;


/**
 * @Author Mr.Wen
 * @Version 1.0
 */
public class TestStreamAPI {
    @Test
    void test1() {
        /*filter 过滤,从流中排除某些元素*/
        List<Employee> employees = EmployeeData.getEmployees();
        Stream<Employee> stream = employees.stream();
        stream.filter(emp -> emp.getSalary() > 6000).forEach(System.out::println);

    }

    @Test
    void test2() {
        long l1 = System.currentTimeMillis();
        /*limit 截断流*/   /*stream是一次性的,一旦使用了终止操作forEach就不可再次使用*/
//        stream.limit(2).forEach(System.out::println);
        List<Employee> employees = EmployeeData.getEmployees();
        Stream<Employee> stream = employees.stream();
        stream.filter(emp -> emp.getSalary() > 6000).limit(2).forEach(System.out::println);
        System.out.println("-------------跳过元素-------------");
        /*skip() 跳过元素*/
        Stream<Employee> stream1 = employees.stream();
        stream1.skip(3).forEach(System.out::println);
//        去重distinct
        System.out.println("-------------去重distinct-------------");
        employees.add(new Employee(101, "特朗普", 50, 6000));
        employees.add(new Employee(101, "特朗普", 50, 6000));
        employees.add(new Employee(101, "特朗普", 50, 6000));
        employees.add(new Employee(101, "特朗普", 50, 6000));
        employees.stream().distinct().forEach(System.out::println);


        long l2 = System.currentTimeMillis();
        System.out.println("程序运行时间：" + (l2 - l1) + "ms");
    }

    @Test/*映射*/
    void test3() {
        long l1 = System.currentTimeMillis();
        List<Employee> employees = EmployeeData.getEmployees();
//      map接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素
        List<String> list = Arrays.asList("a", "b", "c");
        list.stream().map(str -> str.toUpperCase(Locale.ROOT)).forEach(System.out::println);
        System.out.println("----------");
        list.stream().map(String::toUpperCase).forEach(System.out::println);
//      姓名长度＞3的员工
        employees.stream().filter(emp -> emp.getName().length() > 3).map(str -> str.getName()).forEach(System.out::println);
        employees.stream().map(Employee::getName).filter(name -> name.length() > 3).forEach(System.out::println);


        long l2 = System.currentTimeMillis();
        System.out.println("程序运行时间：" + (l2 - l1) + "ms");
    }

    @Test
    void test4() {
        List<Employee> employees = EmployeeData.getEmployees();
        Integer[] arr = new Integer[]{123, 65456, 12, 8, 545};
        String[] strs = new String[]{"AA", "GG", "LL", "MM"};
        Arrays.stream(arr).sorted().forEach(System.out::println);
        System.out.println(Arrays.toString(arr));/*Stream不会改变原对象,他会返回一个持有结果的新的stream*/
        System.out.println();
        Arrays.stream(strs).sorted().forEach(System.out::println);
//        定制化sorted(comparator)
        System.out.println("------------");
        employees.stream().sorted((e1, e2) -> e1.getAge() - e2.getAge()).forEach(System.out::println);
        System.out.println();
        employees.stream().sorted((e1, e2) -> -e1.compareTo(e2)).forEach(System.out::println);
        System.out.println();
        Arrays.stream(strs).sorted(String::compareTo).forEach(System.out::println);

    }

}
